/**
 * 
 */
package com.example.ad_project;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.json.JSONException;

import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.nus.iss.ad.dbhelper.DisbursementDBHelper;
import com.nus.iss.ad.model.DeptRequest;
import com.nus.iss.ad.model.DeptRequestDetails;
import com.nus.iss.ad.model.DisbursementListModel;
import com.nus.iss.ad.utils.ExpandableListAdapter;

/**
 * @author Sachin
 * 
 */
public class DisbursmentDetailActivity extends BaseActivity {

	//

	private int selectedCollectionId;

	DisbursementListModel dModel;

	DisbursementDBHelper data = new DisbursementDBHelper();

	ExpandableListAdapter listAdapter;
	ExpandableListView expListView;
	List<DeptRequest> listDataHeaderDep;
	// List<String> listDataHeader;
	HashMap<String, List<DeptRequestDetails>> listDataChild;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.disbursment_details_layout);

		// collectionPointId
		Bundle extras = getIntent().getExtras();
		// to get the extra from the call
		if (extras != null) {
			selectedCollectionId = Integer.parseInt(extras
					.getString("collectionPointId"));
			
		}

		try {
			dModel = data
					.getDisbursementListModelforCollectionPt(selectedCollectionId);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// get the listview
		expListView = (ExpandableListView) findViewById(R.id.lvExp);

		// preparing list data
		prepareListData(dModel);

		listAdapter = new ExpandableListAdapter(this, listDataHeaderDep,
				listDataChild);

		// setting list adapter
		expListView.setAdapter(listAdapter);
	}

	/*
	 * Preparing the list data
	 */
	private void prepareListData(DisbursementListModel model) {
		// listDataHeader = new ArrayList<String>();
		listDataChild = new HashMap<String, List<DeptRequestDetails>>();
		listDataHeaderDep = new ArrayList<DeptRequest>();

		// Adding Header data
		int i = 0;
		for (DeptRequest d : model.getDeptRequest()) {

			// adding the Dept Name
			// listDataHeader.add(d.getDepartmentName());
			listDataHeaderDep.add(d);

			// adding child data
			listDataChild.put(listDataHeaderDep.get(i).getDepartmentName(),
					d.getDeptRequestDetails());

			i++;

		}

		System.out.println(listDataChild);

	}

	public boolean checkListForProperValues() {

		boolean retValue = false;

		HashMap<String, List<DeptRequestDetails>> newHashMap = listAdapter
				.get_listDataChild();

		Set<String> keyList = newHashMap.keySet();

		for (String key : keyList) {

			for (DeptRequestDetails d : newHashMap.get(key)) {

				if (d.isCheckFlag()) {
					retValue = true;
				}

			}
		}

		return retValue;

	}

	public void onBackPressed() {

		if (checkListForProperValues()) {
			
			Toast.makeText(getApplicationContext(), " Please enter proper Values ",
					Toast.LENGTH_SHORT).show();

		} else {
			
			// saving to local json
			saveToLocalJSON();
			super.onBackPressed();
			// showDialog("Data Saved on Phone");
			Toast.makeText(getApplicationContext(), " Data Saved Temporarily",
					Toast.LENGTH_SHORT).show();

		}
	}
	
	
	public void saveToLocalJSON() {
		
		HashMap<String, List<DeptRequestDetails>> newHashMap = listAdapter
				.get_listDataChild();

		Set<String> keyList = newHashMap.keySet();

		for (String key : keyList) {

			for (DeptRequestDetails d : newHashMap.get(key)) {

				try {
					data.updateJSON(selectedCollectionId, d.getDeptReqId(), d.getQtyDelivered());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				

			}
		}
		
	}

}
